<?php
/** ***********************
 * 作者：Zpy
 * 日期：2016/11/29
 * 作用：票++发票类
 ** ***********************/
class fapiao{
	public $host;
	public $port;
	public $taxpayerId;
	public $taxpayerKey;
	public $timestamp;
	public $buiness_name;

	function __construct($config){
		$this->host     	=  $config['host'];
		$this->port  		=  $config['port'];
		$this->taxpayerId   =  $config['taxpayerId'];
		$this->taxpayerKey  =  $config['taxpayerKey'];
		$this->timestamp    =  time()*1000;
		$this->buiness_name =  $config['buiness_name'];
		
	}
	function test(){
	
		return  'test-ok';
	}
	//开票
	function openTicket($data){
		
		$url = $this->host.':'.$this->port.'/icloud/invoice/issue.json';
		
		$sign_data = array(
			'taxpayerId' 	=> $this->taxpayerId,//纳税人识别号
			'buyerName'		=> $this->buiness_name,//购买方名称
			'collectorPhone'=> $data['collectorPhone'],//收票人手机
			'drawer'		=> $data['drawer'],//开票人
			'orderNo'		=> $data['orderNo'],//订单号
		);
		$sign = $this->sign('POST',$url,$sign_data);
		$url = 'http://'.$this->host.':'.$this->port.'/icloud/invoice/issue.json?timestamp='.$this->timestamp.'&sign='.$sign;
		$post_data = array(
			
			'taxpayerId' 		=> $this->taxpayerId,//纳税人识别号
			'buyerName'			=> $this->buiness_name,//购买方名称
			'collectorPhone'	=> $data['collectorPhone'],//收票人手机
			'drawer'			=> $data['drawer'],//开票人
			'orderNo'			=> $data['orderNo'],//订单号
			'buyerTaxpayerId'	=> $data['buyerTaxpayerId'],//购买方税号
			'buyerAccountNo'	=> $data['buyerAccountNo'],//购买方开户信息
			'buyerAddr'			=> $data['buyerAddr'],//购买方地址
			'wareListInfo'		=> $data['wareListInfo'] //商品信息
		);
		
//		print_r($post_data);
		
		
		$resault = $this->curlrequest($url,$post_data,'POST');
		//print_r($resault);
		return $this->jsontoarr($resault);
	}
	
	//查询
	function query($data){
		
		$url = $this->host.':'.$this->port.'/icloud/invoice/query.json';
		
		$sign_data = array(
			'taxpayerId' 	=> $this->taxpayerId,//纳税人识别号
			'invoiceCode'	=> $data['invoiceCode'],//发票代码
			'invoiceNum'	=> $data['invoiceNum'],//发票号码
			'requestCode'	=> $data['requestCode'],//请求流水号
			'orderNo'		=> $data['orderNo'],//订单号
		);
		$sign = $this->sign('POST',$url,$sign_data);
		$url = 'http://'.$this->host.':'.$this->port.'/icloud/invoice/query.json?timestamp='.$this->timestamp.'&sign='.$sign;
		$post_data = array(
			
			'taxpayerId' 		=> $this->taxpayerId,//纳税人识别号	1
			'authedTaxpayerId'	=> $data['authedTaxpayerId'],//授权平台纳税人识别号  	0
			'requestCode'		=> $data['requestCode'],//请求流水号   	        0
			'invoiceNum'		=> $data['invoiceNum'],//发票号码					0
			'invoiceCode'		=> $data['invoiceCode'],//发票代码			0
			'orderNo'			=> $data['orderNo'],//订单号			0
			'needItem'			=> $data['needItem'] ? true : false,//是否需要商品详情  
			
		);
	
		$resault = $this->curlrequest($url,$post_data,'POST');
	
		return $this->jsontoarr($resault);
	}
	//冲红
	function revoke($data){
		$url = $this->host.':'.$this->port.'/icloud/invoice/revoke.json';
		
		$sign_data = array(
			'taxpayerId'			=> $this->taxpayerId,//纳税人识别号
			'orderNo' 				=> $data['orderNo'],//订单号
			'replacedInvoiceCode'	=> $data['replacedInvoiceCode'],//原发票代码
			'replacedInvoiceNo'		=> $data['replacedInvoiceNo'],//原发票号码
			'revokeReson'			=> $data['revokeReson'],//冲红原因
			
		);
		$sign = $this->sign('POST',$url,$sign_data);
		$url = 'http://'.$this->host.':'.$this->port.'/icloud/invoice/revoke.json?timestamp='.$this->timestamp.'&sign='.$sign;
		$post_data = array(
			
			'taxpayerId' 			=> $this->taxpayerId,//纳税人识别号	1
			'orderNo' 				=> $data['orderNo'],//订单号
			'replacedInvoiceCode'	=> $data['replacedInvoiceCode'],//原发票代码
			'replacedInvoiceNo'		=> $data['replacedInvoiceNo'],//原发票号码
			'revokeReson'			=> $data['revokeReson'],//冲红原因
			
			
		);
//		print_r($post_data);
		
		$resault = $this->curlrequest($url,$post_data,'POST');
//		print_r($resault);
		return $this->jsontoarr($resault);
	}
	//开票统计
	function querybillingcount($data){
		$url = $this->host.':'.$this->port.'/icloud/invoice/querybillingcount.json';
		
		$sign_data = array(
			'taxpayerId'	=> $this->taxpayerId,//纳税人识别号
			'queryDate'		=> $data['queryDate'],//YYYYMM 统计月份
			
			
		);
		$sign = $this->sign('POST',$url,$sign_data);
		$url = 'http://'.$this->host.':'.$this->port.'/icloud/invoice/querybillingcount.json?timestamp='.$this->timestamp.'&sign='.$sign;
		$post_data = array(
			'taxpayerId'			=> $this->taxpayerId,//纳税人识别号
			'authedTaxpayerId' 		=> '',//授权平台纳税人识别号
			'queryDate'		=> $data['queryDate'],//YYYYMM 统计月份
			
			
		);
		
		$resault = $this->curlrequest($url,$post_data,'POST');
//		print_r($resault);
		return $this->jsontoarr($resault);
	}
	//查询余票
	function queryBillLeftCount($data){
		$url = $this->host.':'.$this->port.'/icloud/invoice/queryBillLeftCount.json';
		
		$sign_data = array(
			'taxpayerId'	=> $this->taxpayerId,//纳税人识别号
		);
		$sign = $this->sign('POST',$url,$sign_data);
		$url = 'http://'.$this->host.':'.$this->port.'/icloud/invoice/queryBillLeftCount.json?timestamp='.$this->timestamp.'&sign='.$sign;
		$post_data = array(
			
			'taxpayerId' 	=> $this->taxpayerId,//纳税人识别号	
		
			
		);
		
		$resault = $this->curlrequest($url,$post_data,'POST');
//		print_r($resault);
		return $this->jsontoarr($resault);
	}
	//签名
	function sign($method,$url,$data){
		
		ksort($data);
		$str = '';
		$data['timestamp'] = $this->timestamp;
		foreach($data as $key=>$value)
		{
			if ($value){
				$str .= $key.'='.$value;
			}
			
		}
		$sign=MD5(urlencode($method.$url.$str.$this->taxpayerKey));
		return $sign;
	}
	//JSON转数组
	function jsontoarr($json){
		return json_decode($json,true);
	}
	//CURL请求
	function curlrequest($url,$data,$method='POST'){
		
		
		
		$ch = curl_init(); //初始化CURL句柄 
		curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串，而不是直接输出 
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
		curl_setopt($ch, CURLOPT_TIMEOUT,5);  
//		$data = json_encode($data,JSON_UNESCAPED_UNICODE);
		$data = json_encode($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json; charset=utf-8',
		    'Content-Length: ' . strlen($data))
		);
		$document = curl_exec($ch);//执行预定义的CURL 
		if(!curl_errno($ch)){ 
		  $info = curl_getinfo($ch); 
			//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; 
			
		} else { 
//		  echo 'Curl error: ' . curl_error($ch); 
			$document = false;
		}
		curl_close($ch);
		return $document;
	}
	
	function curl_post($url,$is_post=0,$post_data,$pem=null){
	
		$ch = curl_init();
		//curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
		//设置header
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		//要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		if ($pem){
			curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
			curl_setopt($ch,CURLOPT_SSLCERT,$pem['cert_path']);
			curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
			curl_setopt($ch,CURLOPT_SSLKEY, $pem['key_path']);
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		//post提交方式
		if ($is_post){
			curl_setopt($ch, CURLOPT_POST, TRUE);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
		
		$data = curl_exec($ch);
		//返回结果
		if($data){
			curl_close($ch);
			return $data;
		} else { 
			$error = curl_errno($ch);
			curl_close($ch);
			return $error;
			
		}
	}
	
	
}